﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using CdcSoftware.Pivotal.Engine.Client.ClientTasks.Attributes;
using Microsoft.PointOfService;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Data;
using System.Windows.Forms;

namespace Pcm.Retail.Client.FormTasks.CommandHandlers
{
    public class RE_TenderCommandHandlers : CommandClientTask
    {
        private DataRow[] loggedInCashier = null;  

        /// <summary>
        /// Test if the Printer is working fine or not by firing a test print.
        /// </summary>
        [ClientTaskCommand]
        public virtual void TestPrinter()
        {
            ICenterContentView centerContentView = ClientContext.ClientAPIService.GetActiveContentObject() as ICenterContentView;
            try
            {
                //Disable the Task Item "Test Print".
                centerContentView.TaskPad.TaskPadGroups["RE.Default.Actions"].Items["RE.CT.TestPrint"].Enabled = false;
                PosHardware.PerformActivity<PivotalPrinter>("PosPrinter", true, null, false, false, false, null, null, null, null, null);
            }
            catch (Exception ex)
            {
                Globals.HandleException(ex);
            }
            finally
            {
                //Enable the Task Item "Open Drawer".
                centerContentView.TaskPad.TaskPadGroups["RE.Default.Actions"].Items["RE.CT.TestPrint"].Enabled = true;
            }
        }

        /// <summary>
        /// Open the cash drawer.
        /// </summary>
        [ClientTaskCommand]
        public virtual void OpenDrawer()
        {
            ICenterContentView centerContentView = ClientContext.ClientAPIService.GetActiveContentObject() as ICenterContentView;
            try
            {
                //Disable the Task Item "Open Drawer".
                centerContentView.TaskPad.TaskPadGroups["RE.Default.Actions"].Items["RE.NewRec.OpenDrawer"].Enabled = false;
                DataTable cashierLogin = Globals.GetDataTable("RE_Cashier Log for Today for Current POS", null, new string[] { "Staff_Id", "End_Time", "Store_Id", "POS_Id", "Batch_Id" });
                loggedInCashier = cashierLogin.AsEnumerable().Where(a => Convert.IsDBNull(a["End_Time"])).ToArray();

                //One Cashier logged in
                if (loggedInCashier.Count() == 1)
                {
                    this.SystemClient.ExecuteServerTask("RE_Store", "CreateDrawerLog", new Type[] { typeof(object), typeof(object), typeof(object), typeof(object) },
                     new object[] {  loggedInCashier[0]["Staff_Id"], loggedInCashier[0]["Store_Id"], loggedInCashier[0]["POS_Id"],
                    loggedInCashier[0]["Batch_Id"] });

                    PosHardware.PerformActivity<PivotalCashDrawer>("CashDrawer", true, null, false, false, false, null, null, null, null, null);
                }
                else if (loggedInCashier.Count() > 1)
                {
                    PivotalMessageBox.Show("Multiple Cashiers are found logged in to the POS. Please contact the Administrator.", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                //show the cashier login prompt
                else
                {
                    if (centerContentView is IFormView)
                        this.FormView.CloseForm(true);

                    if (PivotalMessageBox.Show("No Cashier found logged in to the POS.", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK)
                        Globals.NewForm("RE_Cashier_Log", null);
                    return;
                }
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
            finally
            {
                //Enable the Task Item "Open Drawer".
                centerContentView.TaskPad.TaskPadGroups["RE.Default.Actions"].Items["RE.NewRec.OpenDrawer"].Enabled = true;
            }
        }
    }
}
